package com.huawei.esdk.tp.device.smc;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.ApplicationContextUtil;
import com.huawei.esdk.platform.common.utils.DateUtils;
import com.huawei.esdk.platform.commu.itf.ICXFSOAPProtocolAdapter;
import com.huawei.esdk.platform.exception.ProtocolAdapterException;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.device.smc.convert.TPConfMgrConvert;
import com.huawei.esdk.tp.device.smc.convert.TPSiteCtrConvert;
import com.huawei.esdk.tp.domain.model.Conference;
import com.huawei.esdk.tp.domain.model.PeriodicalConference;
import com.huawei.esdk.tp.domain.model.ScheduleConference;
import com.huawei.esdk.tp.domain.model.Site;
import com.huawei.esdk.tp.domain.model.SiteInConference;
import com.huawei.esdk.tp.southcommu.cxf.gen.AddSiteInScheduledConfResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.AddSiteInScheduledConfSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.ConferenceIdSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.ConnectSitesResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.ConnectSitesSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.DelSiteInScheduledConfResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.DelSiteInScheduledConfSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.DisconnectSitesResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.DisconnectSitesSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.EditFutureConfResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.EditFutureConfSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.MuteSitesResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.MuteSitesSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.ProlongScheduledConfResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.ProlongScheduledConfSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.QueryConfSitesStatusResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.QueryConfSitesStatusSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.QueryConferencesStatusResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.QueryConferencesStatusSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.QuietSitesResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.QuietSitesSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.ScheduleConfResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.ScheduleConfSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.SmcExternal2SoapSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.TimeSpanExSouth;

public class ConferenceManagerCapabilityImpl extends V1R1C01BaseCapability implements
        IConferenceManagerCapability
{

    protected static final Logger LOGGER = Logger
            .getLogger(ConferenceManagerCapabilityImpl.class);

    public ConferenceManagerCapabilityImpl(
            ICXFSOAPProtocolAdapter cxfSOAPProtocolAdapter)
    {
        super(cxfSOAPProtocolAdapter);
    }

    private TPConfMgrConvert tpConfMgrConvertService = ApplicationContextUtil
            .getBean("tpConfMgrConvertService");

    @Override
    public SDKErrorCode connectSites(String confID, List<String> sitesUri)
    {
        SDKErrorCode result = new SDKErrorCode();
        // 生成协议相关的Bean
        ConnectSitesSouth request = TPSiteCtrConvert.connectSitesParamConvert(
                confID, sitesUri);
        // 南向消息发送
        ConnectSitesResponseSouth responseSouth = null;
        try
        {
            responseSouth = (ConnectSitesResponseSouth) sendProtocolMessage(
                    request, "connectSites");
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        // 校验设备返回消息
        if (null == responseSouth)
        {
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        else
        {
            result.setErrCode(responseSouth.getConnectSitesResult()
                    .getErrCode());
        }
        return result;
    }

    @Override
    public SDKErrorCode disconnectSites(String confID, List<String> sitesUri)
    {
        SDKErrorCode result = new SDKErrorCode();
        // 生成协议相关的Bean
        DisconnectSitesSouth request = TPSiteCtrConvert
                .disconnectSitesParamConvert(confID, sitesUri);
        // 南向消息发送
        DisconnectSitesResponseSouth responseSouth = null;
        try
        {
            responseSouth = (DisconnectSitesResponseSouth) sendProtocolMessage(
                    request, "disconnectSites");
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        // 校验设备返回消息
        if (null == responseSouth)
        {
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        else
        {
            result.setErrCode(responseSouth.getDisconnectSitesResult()
                    .getErrCode());
        }
        return result;
    }

    @Override
    public SDKErrorCode quietSites(String confID, List<String> sitesUri,
            boolean actionCode)
    {
        SDKErrorCode result = new SDKErrorCode();
        // 生成协议相关的Bean
        QuietSitesSouth request = TPSiteCtrConvert.quietSitesParamConvert(
                confID, sitesUri, actionCode);
        // 南向消息发送
        QuietSitesResponseSouth responseSouth;
        try
        {
            responseSouth = (QuietSitesResponseSouth) sendProtocolMessage(
                    request, "quietSites");
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        // 校验设备返回消息
        if (null == responseSouth)
        {
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        else
        {
            result.setErrCode(responseSouth.getQuietSitesResult().getErrCode());
        }
        return result;
    }

    @Override
    public SDKErrorCode muteSites(String confID, List<String> sitesUri,
            boolean actionCode)
    {
        SDKErrorCode result = new SDKErrorCode();
        // 生成协议相关的Bean
        MuteSitesSouth request = TPSiteCtrConvert.muteSitesParamConvert(confID,
                sitesUri, actionCode);
        // 南向消息发送
        MuteSitesResponseSouth responseSouth = null;
        try
        {
            responseSouth = (MuteSitesResponseSouth) sendProtocolMessage(
                    request, "muteSites");
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        // 校验设备返回消息
        if (null == responseSouth)
        {
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        else
        {
            result.setErrCode(responseSouth.getMuteSitesResult().getErrCode());
        }
        return result;
    }

    @Override
    public SDKResult<List<SiteInConference>> queryConfSitesStatus(
            String confId, List<String> siteUris)
    {
        // modelbean 转南向请求对象
        QueryConfSitesStatusSouth reqBean = tpConfMgrConvertService
                .queryConfSitesStatusModel2Soap(confId, siteUris);

        // 南向产品bean的名称：QueryConfSitesStatus
        QueryConfSitesStatusResponseSouth res = null;
        SDKResult<List<SiteInConference>> result = new SDKResult<List<SiteInConference>>();
        try
        {
            res = (QueryConfSitesStatusResponseSouth) sendProtocolMessage(
                    reqBean, "queryConfSitesStatus");
            result = tpConfMgrConvertService.queryConfSiteStatusSoap2model(res);
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }

        return result;
    }

    @Override
    public SDKResult<List<Conference>> queryConfStatus(List<String> confIds)
    {
        SDKResult<List<Conference>> result = new SDKResult<List<Conference>>();
        QueryConferencesStatusSouth south = tpConfMgrConvertService
                .list2QueryConferencesStatusSouth(confIds);
        QueryConferencesStatusResponseSouth response = null;
        try
        {
            response = (QueryConferencesStatusResponseSouth) sendProtocolMessage(
                    south, "queryConferencesStatus");
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }

        if (null == response)
        {
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        else
        {
            result = tpConfMgrConvertService
                    .queryConferencesStatusResponseSouth2SDKResult(response);
        }
        return result;
    }

    @Override
    public SDKResult<ScheduleConference> addSite(String confId, Site site,
            Date beginTime, boolean useAnonymousSite)
    {
        SDKResult<ScheduleConference> modelBean = new SDKResult<ScheduleConference>();

        // 1.领域Bean转成南向SOAP Bean
        ScheduleConference conference = new ScheduleConference(confId);


        // 2.向SMC发SOAP消息
        AddSiteInScheduledConfResponseSouth southResponse = null;
        try
        {
            AddSiteInScheduledConfSouth southSoap = tpConfMgrConvertService
                .addSiteToScheduledConfModel2SouthSoap(conference, site,
                        beginTime, useAnonymousSite);
            
            southResponse = (AddSiteInScheduledConfResponseSouth) cxfSOAPProtocolAdapter
                    .syncSendMessageWithCxf(
                            southSoap,
                            SmcExternal2SoapSouth.class.getName(),
                            "addSiteInScheduledConf");
        }
        catch (ProtocolAdapterException e)
        {
            // 打日志
            LOGGER.error("addSite method error: " + e);
            modelBean.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            return modelBean;
        }
        catch (SDKException e) {
            modelBean.setErrCode(e.getSdkErrCode());
            return modelBean;
        }

        // 3.将SMC返回的南向SOAP Bean转成领域Bean
        modelBean = tpConfMgrConvertService
                .addSiteToScheduledConfSouthSoap2Model(conference, site,
                        southResponse);

        return modelBean;
    }

    @Override
    public SDKErrorCode removeSite(String confId, String siteUri)
    {
        // 1.领域Bean转成南向SOAP Bean
        DelSiteInScheduledConfSouth southRequest = tpConfMgrConvertService
                .delSiteFromScheduledConfModel2Soap(confId, siteUri);

        // 2.向SMC发SOAP消息
        DelSiteInScheduledConfResponseSouth southResponse = null;
        SDKErrorCode errorCode = new SDKErrorCode();
        try
        {
            southResponse = sendProtocolMessage(southRequest,
                    "delSiteInScheduledConf");
            // 3.将SMC返回的南向SOAP Bean转成领域Bean
            errorCode = tpConfMgrConvertService
                    .delSiteFromScheduledConfSoap2Model(southResponse);
        }
        catch (ProtocolAdapterException e)
        {
            errorCode.setErrCode(e.getErrorCode());
            return errorCode;
        }
        return errorCode;
    }

    @Override
    public SDKErrorCode removeSite(String confId, SiteInConference site)
    {
        // 1.领域Bean转成南向SOAP Bean
        DelSiteInScheduledConfSouth southRequest = tpConfMgrConvertService
                .delSiteFromScheduledConfModel2Soap(confId, site);

        // 2.向SMC发SOAP消息
        DelSiteInScheduledConfResponseSouth southResponse = null;
        SDKErrorCode errorCode = new SDKErrorCode();
        try
        {
            southResponse = sendProtocolMessage(southRequest,
                    "delSiteInScheduledConf");
            // 3.将SMC返回的南向SOAP Bean转成领域Bean
            errorCode = tpConfMgrConvertService
                    .delSiteFromScheduledConfSoap2Model(southResponse);
        }
        catch (ProtocolAdapterException e)
        {
            errorCode.setErrCode(e.getErrorCode());
            return errorCode;
        }
        return errorCode;
    }

    @Override
    public SDKResult<ScheduleConference> scheduleConference(
            ScheduleConference conf)
    {

        SDKResult<ScheduleConference> result = new SDKResult<ScheduleConference>();
        ScheduleConfResponseSouth responseSouth = null;
        try
        {
            ScheduleConfSouth resMsg = tpConfMgrConvertService
                .scheduleConfModel2soap(conf);
            
            responseSouth = (ScheduleConfResponseSouth) sendProtocolMessage(
                    resMsg, "scheduleConf");
            result = tpConfMgrConvertService
                    .scheduleConfSoap2model(responseSouth);
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        catch (SDKException e) {
            result.setErrCode(e.getSdkErrCode());
            return result;
        }

        return result;
    }

    @Override
    public SDKResult<ScheduleConference> editScheduleConference(
            ScheduleConference conf)
    {
        SDKResult<ScheduleConference> result = new SDKResult<ScheduleConference>();
        EditFutureConfResponseSouth responseSouth = null;
        try
        {
            EditFutureConfSouth resMsg = tpConfMgrConvertService
                .editScheduleConfModel2soap(conf);
            
            responseSouth = (EditFutureConfResponseSouth) sendProtocolMessage(
                    resMsg, "editFutureConf");
            result = tpConfMgrConvertService
                    .editScheduleConfSoap2model(responseSouth);
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        catch (SDKException e) {
            result.setErrCode(e.getSdkErrCode());
            return result;
        }

        return result;
    }

    // /////
    @Override
    public SDKResult<PeriodicalConference> scheduleRecurrenceConference(
            PeriodicalConference conf)
    {

        SDKResult<PeriodicalConference> result = new SDKResult<PeriodicalConference>();
        ScheduleConfResponseSouth responseSouth = null;
        try
        {        
            ScheduleConfSouth resMsg = tpConfMgrConvertService
                .scheduleRecurrenceConfModel2soap(conf);
        
            responseSouth = (ScheduleConfResponseSouth) sendProtocolMessage(
                    resMsg, "scheduleConf");
            result = tpConfMgrConvertService
                    .scheduleRecurrenceConfSoap2model(responseSouth);
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }        
        catch (SDKException e) {
            result.setErrCode(e.getSdkErrCode());
            return result;
        }

        return result;
    }

    @Override
    public SDKResult<PeriodicalConference> editScheduleRecurrenceConference(
            PeriodicalConference conf)
    {
        SDKResult<PeriodicalConference> result = new SDKResult<PeriodicalConference>();
        EditFutureConfResponseSouth responseSouth = null;
        try
        {
            EditFutureConfSouth resMsg = tpConfMgrConvertService
                .editSchedulePeriodicalConfModel2soap(conf);
            
            responseSouth = (EditFutureConfResponseSouth) sendProtocolMessage(
                    resMsg, "editFutureConf");
            result = tpConfMgrConvertService
                    .editScheduleRecurrenceConfSoap2model(responseSouth);
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        catch (SDKException e) {
            result.setErrCode(e.getSdkErrCode());
            return result;
        }
        
        return result;
    }

    /**
     * 领域bean转南向soap bean 如果没有返回参数，用SDKErrorCode，只有成功，失败的情况 有返回参数，用SDKResult
     */
    @Override
    public SDKErrorCode prolongScheduleConf(String confId, Date beginDate,
            Integer prolongMinutes)
    {
        // 定义协议相关的Bean
        ProlongScheduledConfSouth reqBean = new ProlongScheduledConfSouth();

        // Domain Bean转成协议相关的Bean
        ConferenceIdSouth conferenceIdSouth = new ConferenceIdSouth();
        conferenceIdSouth.setConfInternalId(Long.parseLong(confId));
        reqBean.setConfId(conferenceIdSouth);

        reqBean.setBeginTime(DateUtils.toGregorianCalendarDate(beginDate));

        TimeSpanExSouth timeSpanExSouth = new TimeSpanExSouth();
        timeSpanExSouth.setTimeSpan("PT" + prolongMinutes + "M");
        reqBean.setProlongTime(timeSpanExSouth);
        // 南向产品bean的名称：ProlongScheduledConf
        ProlongScheduledConfResponseSouth res = null;
        SDKErrorCode result = new SDKErrorCode();
        try
        {
            res = (ProlongScheduledConfResponseSouth) cxfSOAPProtocolAdapter
                    .syncSendMessageWithCxf(reqBean,
                            SmcExternal2SoapSouth.class.getName(),
                            "prolongScheduledConf");
            result.setErrCode(res.getProlongScheduledConfResult().getErrCode());
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }

        return result;
    }
}
